﻿using java.util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Common
{
    public class AudioConverter
    {
        /// <summary>
        /// 将byte数据写入临时文件
        /// </summary>
        /// <param name="pathBefore"></param>
        /// <param name="pathLater"></param>
        /// <returns></returns>
        public static string ConvertByteToMp3(byte[] byteData, string outputFilePath)
        {
            // 构建FFmpeg命令
            // 将byte数据写入临时文件
            string tempFile = "tempfile.data";
            File.WriteAllBytes(tempFile, byteData);

            // FFmpeg命令
            string arguments = $"-i {tempFile} {outputFilePath}";

            string result = RunCmd(arguments);
            if (result == "true") 
            {
                File.Delete(tempFile); // 处理完毕后删除临时文件
            }
            return result;
        }

        /// <summary>
        /// 文件格式转换
        /// </summary>
        /// <param name="pathBefore"></param>
        /// <param name="pathLater"></param>
        /// <returns></returns>
        public static string ConvertToMp3(string inputFilePath, string outputFilePath)
        {
            if (!File.Exists(inputFilePath))
            {
                return "找不到源文件！";
            }
            // 构建FFmpeg命令
            string arguments = $"-i {inputFilePath} {outputFilePath}";
            string result = RunCmd(arguments);
            if (result == "true") 
            {
                File.Delete(inputFilePath); // 删除文件
            }
            return result;
        }
        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="inputFilePath"></param>
        /// <param name="outputFilePath"></param>
        /// <returns></returns>
        public static string CompressFile(string inputFilePath, string outputFilePath)
        {
            if (!System.IO.File.Exists(inputFilePath))
            {
                return "找不到源文件！";
            }
            // 创建一个新的FFmpeg命令
            string arguments = $"-i {inputFilePath} -codec:a libmp3lame -b:a 128k {outputFilePath}";
            // 使用FFmpeg命令行接口执行命令
            var result = RunCmd(arguments);
            return result;
        }

        /// <summary>
        /// 执行Cmd命令
        /// </summary>
        private static string RunCmd(string arguments)
        {
            try
            {
                //取得ffmpeg.exe的物理路径
                string ffmpegPath = AppDomain.CurrentDomain.BaseDirectory + @"Content\\FFmpeg\\ffmpeg.exe";
                if (!System.IO.File.Exists(ffmpegPath))
                {
                    return "找不到格式转换程序！";
                }
                using (Process process = new Process())
                {
                    process.StartInfo.FileName = ffmpegPath;
                    process.StartInfo.Arguments = arguments;
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError = true;
                    process.StartInfo.CreateNoWindow = true;
                    process.Start();

                    string output = process.StandardOutput.ReadToEnd();
                    string error = process.StandardError.ReadToEnd();
                    process.WaitForExit();

                    string result = "false";
                    // 这里可以处理输出或错误信息
                    if (process.HasExited && process.ExitCode == 0)
                    {
                        result ="true";
                    }
                    process.Close();
                    return result;
                }
            }
            catch (Exception ex)
            {
                return "error" + ex.Message;
            }
        }
    }
}
